<?php

namespace app\adminapi\controller;

use think\Collection;
use think\Controller;
use think\Request;
use app\common\model\category as CategoryModel;

class Category extends BaseApi
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = input();
        $where = [];
        if (isset($params['pid'])){
            $where['pid'] = $params['pid'];
        }
        //查询所有
        $list = CategoryModel::where($where)->select();
        $list = (new \think\Collection($list))->toArray();
        if (!isset($params['type']) || $params['type'] != 'list'){
            $list  = get_cate_list($list);
        }
        $this->ok($list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $params = input();
        $validate = $this->validate($params, [
           'cate_name'=>'require',
           'pid'=>'require|egt:0',
           'is_show' => 'require|in:0,1',
            'is_hot'=>'require|in:0,1',
            'sort'=>'require|between:0,99999',
        ]);
        $params['pid_path'] = 0;
        $params['pid_path_name']='';
        $params['level'] = 0;
        $params['image_url'] = $params['logo'];
        if ($params['pid'] != 0){
            $pinfo = CategoryModel::find($params['pid']);
            if (empty($pinfo)){
                $this->fail('数据异常');
            }
            $params['pid_path'] = $pinfo['pid_path'].'_'.$pinfo['id'];
            $params['pid_path_name'] = $pinfo['pid_path'].'_'.$pinfo['cate_name'];
            $params['level'] = $pinfo['level'] + 1;
        }
        $obj = CategoryModel::create($params,true);
        $info = CategoryModel::find($obj['id']);
        $this->ok($info);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        $info = CategoryModel::find($id);
        $this->ok($info);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = input();
        $validate = $this->validate($params, [
            'cate_name'=>'require',
            'pid'=>'require|egt:0',
            'is_show' => 'require|in:0,1',
            'is_hot'=>'require|in:0,1',
            'sort'=>'require|between:0,99999',
        ]);
        $params['pid_path'] = 0;
        $params['pid_path_name']='';
        $params['level'] = 0;
        if (!empty($params['logo'])){
            $params['image_url'] = $params['logo'];
        }
        if ($params['pid'] != 0){
            $pinfo = CategoryModel::find($params['pid']);
            if (empty($pinfo)){
                $this->fail('数据异常');
            }
            $params['pid_path'] = $pinfo['pid_path'].'_'.$pinfo['id'];
            $params['pid_path_name'] = $pinfo['pid_path'].'_'.$pinfo['cate_name'];
            $params['level'] = $pinfo['level'] + 1;
        }
        CategoryModel::update($params,['id',$id],true);
        $info = CategoryModel::find($id);
        $this->ok($info);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //有子分类不能删除
        $total = CategoryModel::where('pid',$id)->count();
        if ($total > 0){
            $this->fail('有子分类,不能删除');
        }
        CategoryModel::destroy($id);
        $info = CategoryModel::find($id);
        $this->ok($info);
    }
}
